import { useUserStore, useRouterStore } from '@/store'

/*
  单点登录
*/
export const useSSOLogin = () => {
  // `location.search` 得到url中?之后#之前的部分
  // 获取当前页面的 URL
  const url = new URL(window.location.href)
  console.log('url', url)
  // 使用 URLSearchParams 提取查询参数
  const params = new URLSearchParams(url.search)
  console.log('params', params)
  // 获取 auth_code 的值
  const token = params.get('auth_code')

  // 输出 auth_code 的值
  console.log('Token:', token)

  const userStore = useUserStore()
  const routerStore = useRouterStore()

  if (token === null || token === undefined) {
    //window.location.href = 'http://10.11.169.153:8102/'
    return
  }
  routerStore.setState({
    loading: true,
    closeLoading: false
  })

  userStore
    .ssoLogin({
      token
    })
    .then(() => {
      routerStore.setState({
        loading: false,
        closeLoading: true
      })
    })
}
